package jsmc.opendata.pratikanant.adapter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import jsmc.opendata.nantes.modele.Parking;
import jsmc.opendata.nantes.util.NeoLog;
import jsmc.opendata.pratikanant.R;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ListParkings extends BaseAdapter {
	
	private Context mactivity = null;
	
	List<Parking> Parkings = null;
	
	public ListParkings(Context activity) {
		
		mactivity = activity;
      
    }
	
	public void SetParkings(List<Parking> p) {
		
		this.Parkings = p;
		
	}
	
	@SuppressWarnings("null")
	public List<Parking> FilterParkings(List<Parking> Parkings_all, String filter) {
				
		List<Parking> Parkings = null;
    	
    	for (int $icont=0;$icont<Parkings_all.size();$icont++) {
    		
    		Parking test = Parkings_all.get($icont);
    		
    		if (test.getNom().toLowerCase().matches("(^|.*\\s)"+filter+".*")) {
    			
    			Parkings.add(test);
    			
    		}
    		
    	}
    	
    	return Parkings;

	}
	
	
	public List<Parking> GetParkings() {
		
		return this.Parkings;
	}

	public int getCount() {

		return this.Parkings.size();
		
	}

	public Object getItem(int arg0) {
		
		return Parkings.get(arg0);
		
	}

	public long getItemId(int position) {

		return position;
		
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		
		if (convertView==null) {
			
			convertView = new  LinearLayout(mactivity);			
			LayoutInflater inflater = (LayoutInflater) mactivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = (LinearLayout) inflater.inflate(R.layout.parking_list_line,(ViewGroup) convertView);
			
		} 
		
		TextView name = (TextView) convertView.findViewById(R.id.name);
		TextView infos = (TextView) convertView.findViewById(R.id.infos);
		
		Parking Parking  = Parkings.get(position);
		
		NeoLog.v("ListParking","getView "+Parking.getNom());
		
		name.setText(Parking.getNom());	
		infos.setText(Parking.getDisponibles()+" places dispo sur "+Parking.getPlacesTotales());
		
		return convertView;
	}

}
